package com.example.controller;

import com.example.common.Result;
import com.example.mapper.MessageMapper;
import com.example.pojo.Message;
import com.example.pojo.PageResult;
import com.example.service.Impl.MessageService;
import com.example.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/messages")
public class MessageController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MessageMapper messageMapper;
    @Resource
    private MessageService messageService;

    /**
     * 标记单个已读
     * @param messageId
     * @param receiverId
     * @param receiverType
     * @return
     */
    @PutMapping("/markAsRead")
    public Result markMessagesAsRead(@RequestParam Integer messageId,@RequestParam Integer receiverId, @RequestParam String receiverType) {
        messageService.markMessagesAsRead(messageId,receiverId, receiverType);
        return Result.success();
    }

    /**
     * 标记全部已读
     * @param receiverId
     * @param receiverType
     * @return
     */
    @PutMapping("/markAllAsRead")
    public Result markAllMessagesAsRead(@RequestParam Integer receiverId, @RequestParam String receiverType) {
        messageService.markAllMessagesAsRead(receiverId, receiverType);
        return Result.success();
    }
    @GetMapping("unRead")
    public Result getUnreadCount(@RequestParam Integer receiverId, @RequestParam String receiverType) {
        Integer count = messageService.getUnreadCount(receiverId, receiverType);
        return Result.success(count);
    }

    @GetMapping("/test")
    public Result test() {
        // 使用LocalDateTime获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("LocalDateTime:"+now);
//        ZonedDateTime shanghai = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        return Result.success("new Date():"+new Date());
    }


    @PostMapping("/testInsert")
    public Result testInsert() {
        Message message  = new Message(
                "test",
                1,
                "appointment",
                1,
                 "用户：test"+"取消了了您的房间：test"
        );
        messageMapper.insert(message);
       return Result.success();
    }

    @GetMapping("/selectAll")
    public Result getMessages(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestHeader("Authorization") String token) {
        // 从token获取用户信息
        Map<String, Object> claims = JwtUtil.parseToken(token);
        String receiverType = (String) claims.get("role");  // 例如："owner" 或 "user"
        Integer receiverId = (Integer) claims.get("id");

        return Result.success(messageService.getMessages(receiverId, receiverType, page, pageSize));
    }

    // 未读数量接口适配
    @GetMapping("/unread")
    public ResponseEntity<Integer> getUnreadCount(
            @RequestHeader("Authorization") String token
    ) {

        Map<String, Object> claims = JwtUtil.parseToken(token);
        String receiverType = (String) claims.get("role");  // 例如："owner" 或 "user"
        Integer receiverId = (Integer) claims.get("id");
        return ResponseEntity.ok(
                messageService.getUnreadCount(receiverId, receiverType)
        );
    }

}
